<template>
  <el-card class="card-top">
    <el-button
      type="primary"
      :icon="Plus"
      @click="emit('toggle', 1)"
      :disabled="!spuStore.$state.category3Id"
      >添加SPU</el-button
    >
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      class="mt-top"
      v-loading="loading"
    >
      <el-table-column label="序号" width="80" type="index" />
      <el-table-column prop="spuName" label="spu名称" width="350" />
      <el-table-column prop="description" label="spu描述" width="350" />

      <el-table-column fixed="right" label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="addSpu(row)"
            :icon="Plus"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            @click="editSpu(row)"
            :icon="Edit"
          ></el-button>
          <el-button
            type="info"
            size="small"
            @click="infoSpu"
            :icon="InfoFilled"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            @click="deleteSpu"
            :icon="Delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="limit"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @size-change="request"
      @current-change="request"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XSpuContent",
};
</script>

<script lang="ts" setup>
import { Plus, Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { reqSpuList } from "@/api/product/attr";
import type { ArrayObjSpu } from "@/api/product/model/index";
import { ref, watch } from "vue";
import { useSpuStore } from "@/stores/spu";
import type { spuInfoType } from "@/stores/interface/index";
const spuStore = useSpuStore();
const emit = defineEmits(["toggle"]);
const page = ref(1);
const limit = ref(3);
const total = ref(50);
const loading = ref(false);
const tableData = ref<ArrayObjSpu>([]);

const handleClick = () => {
  console.log("click");
};
async function request() {
  const category3Id = spuStore.$state.category3Id;
  if (!category3Id) return;
  loading.value = true;
  const result = await reqSpuList(
    page.value,
    limit.value,
    category3Id as number
  );
  tableData.value = result.records;
  total.value = result.total;
  loading.value = false;
  console.log(result);
}
watch(
  () => spuStore.$state.category3Id,
  (newValue) => {
    if (!newValue) return;
    request();
  },
  { immediate: true }
);
function addSpu(row: any) {
  goSpuEdit(row, 3);
}
function editSpu(row: any) {
  goSpuEdit(row, 2);
}
function infoSpu() {
  console.log(222);
}
function deleteSpu() {
  console.log(222);
}
function goSpuEdit(row: any, numberType: number) {
  const { id, description, spuName, tmId } = row;
  spuStore.addSpuInfo({ id, description, spuName, tmId });
  emit("toggle", numberType);
}
</script>

<style scoped>
.card-top {
  margin-top: 10px;
}
</style>
